<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>classlist_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_dom_classlist_test.js.html">classlist_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2012 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Shared code for classlist_test.html.
<a name="line17"></a> */
<a name="line18"></a>
<a name="line19"></a>goog.provide(&#39;goog.dom.classlist_test&#39;);
<a name="line20"></a>goog.setTestOnly(&#39;goog.dom.classlist_test&#39;);
<a name="line21"></a>
<a name="line22"></a>goog.require(&#39;goog.dom&#39;);
<a name="line23"></a>goog.require(&#39;goog.dom.classlist&#39;);
<a name="line24"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line25"></a>
<a name="line26"></a>
<a name="line27"></a>var classlist = goog.dom.classlist;
<a name="line28"></a>
<a name="line29"></a>function testGet() {
<a name="line30"></a>  var el = document.createElement(&#39;div&#39;);
<a name="line31"></a>  assertTrue(classlist.get(el).length == 0);
<a name="line32"></a>  el.className = &#39;C&#39;;
<a name="line33"></a>  assertElementsEquals([&#39;C&#39;], classlist.get(el));
<a name="line34"></a>  el.className = &#39;C D&#39;;
<a name="line35"></a>  assertElementsEquals([&#39;C&#39;, &#39;D&#39;], classlist.get(el));
<a name="line36"></a>  el.className = &#39;C\nD&#39;;
<a name="line37"></a>  assertElementsEquals([&#39;C&#39;, &#39;D&#39;], classlist.get(el));
<a name="line38"></a>  el.className = &#39; C &#39;;
<a name="line39"></a>  assertElementsEquals([&#39;C&#39;], classlist.get(el));
<a name="line40"></a>}
<a name="line41"></a>
<a name="line42"></a>function testContainsWithNewlines() {
<a name="line43"></a>  var el = goog.dom.getElement(&#39;p1&#39;);
<a name="line44"></a>  assertTrue(&#39;Should not have SOMECLASS&#39;, classlist.contains(el, &#39;SOMECLASS&#39;));
<a name="line45"></a>  assertTrue(&#39;Should also have OTHERCLASS&#39;,
<a name="line46"></a>      classlist.contains(el, &#39;OTHERCLASS&#39;));
<a name="line47"></a>  assertFalse(&#39;Should not have WEIRDCLASS&#39;,
<a name="line48"></a>      classlist.contains(el, &#39;WEIRDCLASS&#39;));
<a name="line49"></a>}
<a name="line50"></a>
<a name="line51"></a>function testContainsCaseSensitive() {
<a name="line52"></a>  var el = goog.dom.getElement(&#39;p2&#39;);
<a name="line53"></a>  assertFalse(&#39;Should not have camelcase&#39;,
<a name="line54"></a>      classlist.contains(el, &#39;camelcase&#39;));
<a name="line55"></a>  assertFalse(&#39;Should not have CAMELCASE&#39;,
<a name="line56"></a>      classlist.contains(el, &#39;CAMELCASE&#39;));
<a name="line57"></a>  assertTrue(&#39;Should have camelCase&#39;,
<a name="line58"></a>      classlist.contains(el, &#39;camelCase&#39;));
<a name="line59"></a>}
<a name="line60"></a>
<a name="line61"></a>function testAddNotAddingMultiples() {
<a name="line62"></a>  var el = document.createElement(&#39;div&#39;);
<a name="line63"></a>  classlist.add(el, &#39;A&#39;);
<a name="line64"></a>  assertEquals(&#39;A&#39;, el.className);
<a name="line65"></a>  classlist.add(el, &#39;A&#39;);
<a name="line66"></a>  assertEquals(&#39;A&#39;, el.className);
<a name="line67"></a>  classlist.add(el, &#39;B&#39;, &#39;B&#39;);
<a name="line68"></a>  assertEquals(&#39;A B&#39;, el.className);
<a name="line69"></a>}
<a name="line70"></a>
<a name="line71"></a>function testAddCaseSensitive() {
<a name="line72"></a>  var el = document.createElement(&#39;div&#39;);
<a name="line73"></a>  classlist.add(el, &#39;A&#39;);
<a name="line74"></a>  assertTrue(classlist.contains(el, &#39;A&#39;));
<a name="line75"></a>  assertFalse(classlist.contains(el, &#39;a&#39;));
<a name="line76"></a>  classlist.add(el, &#39;a&#39;);
<a name="line77"></a>  assertTrue(classlist.contains(el, &#39;A&#39;));
<a name="line78"></a>  assertTrue(classlist.contains(el, &#39;a&#39;));
<a name="line79"></a>  assertEquals(&#39;A a&#39;, el.className);
<a name="line80"></a>}
<a name="line81"></a>
<a name="line82"></a>function testAddAll() {
<a name="line83"></a>  var elem = document.createElement(&#39;div&#39;);
<a name="line84"></a>  elem.className = &#39;foo goog-bar&#39;;
<a name="line85"></a>
<a name="line86"></a>  goog.dom.classlist.addAll(elem, [&#39;goog-baz&#39;, &#39;foo&#39;]);
<a name="line87"></a>  assertEquals(3, classlist.get(elem).length);
<a name="line88"></a>  assertTrue(goog.dom.classlist.contains(elem, &#39;foo&#39;));
<a name="line89"></a>  assertTrue(goog.dom.classlist.contains(elem, &#39;goog-bar&#39;));
<a name="line90"></a>  assertTrue(goog.dom.classlist.contains(elem, &#39;goog-baz&#39;));
<a name="line91"></a>}
<a name="line92"></a>
<a name="line93"></a>function testAddAllEmpty() {
<a name="line94"></a>  var classes = &#39;foo bar&#39;;
<a name="line95"></a>  var elem = document.createElement(&#39;div&#39;);
<a name="line96"></a>  elem.className = classes;
<a name="line97"></a>
<a name="line98"></a>  goog.dom.classlist.addAll(elem, []);
<a name="line99"></a>  assertEquals(elem.className, classes);
<a name="line100"></a>}
<a name="line101"></a>
<a name="line102"></a>function testRemove() {
<a name="line103"></a>  var el = document.createElement(&#39;div&#39;);
<a name="line104"></a>  el.className = &#39;A B C&#39;;
<a name="line105"></a>  classlist.remove(el, &#39;B&#39;);
<a name="line106"></a>  assertEquals(&#39;A C&#39;, el.className);
<a name="line107"></a>}
<a name="line108"></a>
<a name="line109"></a>function testRemoveCaseSensitive() {
<a name="line110"></a>  var el = document.createElement(&#39;div&#39;);
<a name="line111"></a>  el.className = &#39;A B C&#39;;
<a name="line112"></a>  classlist.remove(el, &#39;b&#39;);
<a name="line113"></a>  assertEquals(&#39;A B C&#39;, el.className);
<a name="line114"></a>}
<a name="line115"></a>
<a name="line116"></a>function testRemoveAll() {
<a name="line117"></a>  var elem = document.createElement(&#39;div&#39;);
<a name="line118"></a>  elem.className = &#39;foo bar baz&#39;;
<a name="line119"></a>
<a name="line120"></a>  goog.dom.classlist.removeAll(elem, [&#39;bar&#39;, &#39;foo&#39;]);
<a name="line121"></a>  assertFalse(goog.dom.classlist.contains(elem, &#39;foo&#39;));
<a name="line122"></a>  assertFalse(goog.dom.classlist.contains(elem, &#39;bar&#39;));
<a name="line123"></a>  assertTrue(goog.dom.classlist.contains(elem, &#39;baz&#39;));
<a name="line124"></a>}
<a name="line125"></a>
<a name="line126"></a>function testRemoveAllOne() {
<a name="line127"></a>  var elem = document.createElement(&#39;div&#39;);
<a name="line128"></a>  elem.className = &#39;foo bar baz&#39;;
<a name="line129"></a>
<a name="line130"></a>  goog.dom.classlist.removeAll(elem, [&#39;bar&#39;]);
<a name="line131"></a>  assertFalse(goog.dom.classlist.contains(elem, &#39;bar&#39;));
<a name="line132"></a>  assertTrue(goog.dom.classlist.contains(elem, &#39;foo&#39;));
<a name="line133"></a>  assertTrue(goog.dom.classlist.contains(elem, &#39;baz&#39;));
<a name="line134"></a>}
<a name="line135"></a>
<a name="line136"></a>function testRemoveAllSomeNotPresent() {
<a name="line137"></a>  var elem = document.createElement(&#39;div&#39;);
<a name="line138"></a>  elem.className = &#39;foo bar baz&#39;;
<a name="line139"></a>
<a name="line140"></a>  goog.dom.classlist.removeAll(elem, [&#39;a&#39;, &#39;bar&#39;]);
<a name="line141"></a>  assertTrue(goog.dom.classlist.contains(elem, &#39;foo&#39;));
<a name="line142"></a>  assertFalse(goog.dom.classlist.contains(elem, &#39;bar&#39;));
<a name="line143"></a>  assertTrue(goog.dom.classlist.contains(elem, &#39;baz&#39;));
<a name="line144"></a>}
<a name="line145"></a>
<a name="line146"></a>function testRemoveAllCaseSensitive() {
<a name="line147"></a>  var elem = document.createElement(&#39;div&#39;);
<a name="line148"></a>  elem.className = &#39;foo bar baz&#39;;
<a name="line149"></a>
<a name="line150"></a>  goog.dom.classlist.removeAll(elem, [&#39;BAR&#39;, &#39;foo&#39;]);
<a name="line151"></a>  assertFalse(goog.dom.classlist.contains(elem, &#39;foo&#39;));
<a name="line152"></a>  assertTrue(goog.dom.classlist.contains(elem, &#39;bar&#39;));
<a name="line153"></a>  assertTrue(goog.dom.classlist.contains(elem, &#39;baz&#39;));
<a name="line154"></a>}
<a name="line155"></a>
<a name="line156"></a>function testEnable() {
<a name="line157"></a>  var el = goog.dom.getElement(&#39;p1&#39;);
<a name="line158"></a>  classlist.set(el, &#39;SOMECLASS FIRST&#39;);
<a name="line159"></a>
<a name="line160"></a>  assertTrue(&#39;Should have FIRST class&#39;, classlist.contains(el, &#39;FIRST&#39;));
<a name="line161"></a>  assertTrue(&#39;Should have SOMECLASS class&#39;,
<a name="line162"></a>      classlist.contains(el, &#39;SOMECLASS&#39;));
<a name="line163"></a>
<a name="line164"></a>  classlist.enable(el, &#39;FIRST&#39;, false);
<a name="line165"></a>
<a name="line166"></a>  assertFalse(&#39;Should not have FIRST class&#39;, classlist.contains(el, &#39;FIRST&#39;));
<a name="line167"></a>  assertTrue(&#39;Should have SOMECLASS class&#39;,
<a name="line168"></a>      classlist.contains(el, &#39;SOMECLASS&#39;));
<a name="line169"></a>
<a name="line170"></a>  classlist.enable(el, &#39;FIRST&#39;, true);
<a name="line171"></a>
<a name="line172"></a>  assertTrue(&#39;Should have FIRST class&#39;, classlist.contains(el, &#39;FIRST&#39;));
<a name="line173"></a>  assertTrue(&#39;Should have SOMECLASS class&#39;,
<a name="line174"></a>      classlist.contains(el, &#39;SOMECLASS&#39;));
<a name="line175"></a>}
<a name="line176"></a>
<a name="line177"></a>function testEnableNotAddingMultiples() {
<a name="line178"></a>  var el = document.createElement(&#39;div&#39;);
<a name="line179"></a>  classlist.enable(el, &#39;A&#39;, true);
<a name="line180"></a>  assertEquals(&#39;A&#39;, el.className);
<a name="line181"></a>  classlist.enable(el, &#39;A&#39;, true);
<a name="line182"></a>  assertEquals(&#39;A&#39;, el.className);
<a name="line183"></a>  classlist.enable(el, &#39;B&#39;, &#39;B&#39;, true);
<a name="line184"></a>  assertEquals(&#39;A B&#39;, el.className);
<a name="line185"></a>}
<a name="line186"></a>
<a name="line187"></a>function testSwap() {
<a name="line188"></a>  var el = goog.dom.getElement(&#39;p1&#39;);
<a name="line189"></a>  classlist.set(el, &#39;SOMECLASS FIRST&#39;);
<a name="line190"></a>
<a name="line191"></a>  assertTrue(&#39;Should have FIRST class&#39;, classlist.contains(el, &#39;FIRST&#39;));
<a name="line192"></a>  assertTrue(&#39;Should have FIRST class&#39;, classlist.contains(el, &#39;SOMECLASS&#39;));
<a name="line193"></a>  assertFalse(&#39;Should not have second class&#39;, classlist.contains(el, &#39;second&#39;));
<a name="line194"></a>
<a name="line195"></a>  classlist.swap(el, &#39;FIRST&#39;, &#39;second&#39;);
<a name="line196"></a>
<a name="line197"></a>  assertFalse(&#39;Should not have FIRST class&#39;, classlist.contains(el, &#39;FIRST&#39;));
<a name="line198"></a>  assertTrue(&#39;Should have FIRST class&#39;, classlist.contains(el, &#39;SOMECLASS&#39;));
<a name="line199"></a>  assertTrue(&#39;Should have second class&#39;, classlist.contains(el, &#39;second&#39;));
<a name="line200"></a>
<a name="line201"></a>  classlist.swap(el, &#39;second&#39;, &#39;FIRST&#39;);
<a name="line202"></a>
<a name="line203"></a>  assertTrue(&#39;Should have FIRST class&#39;, classlist.contains(el, &#39;FIRST&#39;));
<a name="line204"></a>  assertTrue(&#39;Should have FIRST class&#39;, classlist.contains(el, &#39;SOMECLASS&#39;));
<a name="line205"></a>  assertFalse(&#39;Should not have second class&#39;, classlist.contains(el, &#39;second&#39;));
<a name="line206"></a>}
<a name="line207"></a>
<a name="line208"></a>function testToggle() {
<a name="line209"></a>  var el = goog.dom.getElement(&#39;p1&#39;);
<a name="line210"></a>  classlist.set(el, &#39;SOMECLASS FIRST&#39;);
<a name="line211"></a>
<a name="line212"></a>  assertTrue(&#39;Should have FIRST class&#39;, classlist.contains(el, &#39;FIRST&#39;));
<a name="line213"></a>  assertTrue(&#39;Should have SOMECLASS class&#39;, classlist.contains(el, &#39;SOMECLASS&#39;));
<a name="line214"></a>
<a name="line215"></a>  var ret = classlist.toggle(el, &#39;FIRST&#39;);
<a name="line216"></a>
<a name="line217"></a>  assertFalse(&#39;Should not have FIRST class&#39;, classlist.contains(el, &#39;FIRST&#39;));
<a name="line218"></a>  assertTrue(&#39;Should have SOMECLASS class&#39;, classlist.contains(el, &#39;SOMECLASS&#39;));
<a name="line219"></a>  assertFalse(&#39;Return value should have been false&#39;, ret);
<a name="line220"></a>
<a name="line221"></a>  ret = classlist.toggle(el, &#39;FIRST&#39;);
<a name="line222"></a>
<a name="line223"></a>  assertTrue(&#39;Should have FIRST class&#39;, classlist.contains(el, &#39;FIRST&#39;));
<a name="line224"></a>  assertTrue(&#39;Should have SOMECLASS class&#39;, classlist.contains(el, &#39;SOMECLASS&#39;));
<a name="line225"></a>  assertTrue(&#39;Return value should have been true&#39;, ret);
<a name="line226"></a>}
<a name="line227"></a>
<a name="line228"></a>function testAddRemoveString() {
<a name="line229"></a>  var el = document.createElement(&#39;div&#39;);
<a name="line230"></a>  el.className = &#39;A&#39;;
<a name="line231"></a>
<a name="line232"></a>  classlist.addRemove(el, &#39;A&#39;, &#39;B&#39;);
<a name="line233"></a>  assertEquals(&#39;B&#39;, el.className);
<a name="line234"></a>
<a name="line235"></a>  classlist.addRemove(el, &#39;Z&#39;, &#39;C&#39;);
<a name="line236"></a>  assertEquals(&#39;B C&#39;, el.className);
<a name="line237"></a>
<a name="line238"></a>  classlist.addRemove(el, &#39;C&#39;, &#39;D&#39;);
<a name="line239"></a>  assertEquals(&#39;B D&#39;, el.className);
<a name="line240"></a>
<a name="line241"></a>  classlist.addRemove(el, &#39;D&#39;, &#39;B&#39;);
<a name="line242"></a>  assertEquals(&#39;B&#39;, el.className);
<a name="line243"></a>}
</pre>


</body>
</html>
